package com.android.bookgarden.ui;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.baselibs.constract.RealtConstract;
import com.android.baselibs.network.HttpType;
import com.android.baselibs.persenter.BasePersenter;
import com.android.bookgarden.R;
import com.android.bookgarden.apis.Apis;
import com.android.bookgarden.base.BaseActivity;
import com.android.bookgarden.bean.DataBean;
import com.android.bookgarden.utli.LogUtil;
import com.android.bookgarden.utli.Utlis;
import com.android.bookgarden.views.LoadingDialog;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.uuzuche.lib_zxing.activity.QRCodeActivity;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;

/**
 * @author XLC
 * @description 家庭教育金
 * @time 2018-11-28
 */
public class FamilyEducationFundUI extends BaseActivity implements View.OnClickListener,RealtConstract.View {

    @BindView(R.id.close)
    ImageView close;
    @BindView(R.id.title_text)
    TextView titleText;

    @BindView(R.id.balance)
    TextView balanceTxt;//账户余额
    @BindView(R.id.scan)
    TextView scanBtn;//扫一扫
    @BindView(R.id.recharge)
    TextView rechargeBtn;//充值
    @BindView(R.id.records_consumption)
    TextView recordsBtn;//交易明细
    @BindView(R.id.ritgh_icon)
    ImageView ritghIcon;
    @BindView(R.id.ritgh_text)
    TextView ritghText;
    private RealtConstract.Presenter presenter;
    private boolean isOpen;

    @Override
    protected int getLayoutId() {
        return R.layout.family_education_fund_ui;
    }

    @Override
    protected void initView() {
        presenter = new BasePersenter(this,this);
        titleText.setText("家庭教育金");
        close.setOnClickListener(this);
        rechargeBtn.setOnClickListener(this);
        scanBtn.setOnClickListener(this);
        recordsBtn.setOnClickListener(this);
        LoadingDialog.showMassge(this,"加载中...");
        presenter.loadDataHeader(null, "", Apis.IS_OPEN  , 1001, Utlis.getHeadler(this), HttpType.GET);


    }

    @Override
    public void onRequestPermissions(String str, boolean z) {
        if (z){
            startActivityForResult(QRCodeActivity.class,5);
//            startActivity(QRCodeActivity.class);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.close:
                finish();
                break;
            case R.id.recharge:
                if (isOpen){
                    startActivity(RechargeUI.class);
                }

                break;
            case R.id.scan:
                checkSelfPermissions("1",new String[]{Manifest.permission.CAMERA});
                break;

            case R.id.records_consumption:
                startActivity(TransactionDetailsUI.class);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (null==data){
            return;
        }
        switch (requestCode){
            case 5:
                String result = data.getStringExtra(CodeUtils.RESULT_STRING);
                Bundle bundle = new Bundle();
                bundle.putString("code_result",result);
                startActivity(PaymentUI.class,bundle);
                break;
        }
    }

    @Override
    public void returnData(String data, int urlCode) {
        LogUtil.e("返回数据", data + "");
        if (TextUtils.isEmpty(data)) {
            return;
        }
        LoadingDialog.hideProgress();
        handler.obtainMessage(urlCode, data).sendToTarget();
    }

    @Override
    public void LoadError(String errorMsg, int urlCode) {
        showToast("请求失败");
        LoadingDialog.hideProgress();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String data = (String) msg.obj;
            switch (msg.what) {


                case 1001:
//                    refreshLayout.setRefreshing(false);
                    DataBean bean = Utlis.getDataBean(data);
                    if (null == bean) {

                        return;
                    }
                    if (bean.getCode() == 200) {
                        try {
                            JSONObject object = new JSONObject(bean.getData());
                            isOpen = object.getBoolean("isopen");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    } else {

                    }


                    break;



            }
        }
    };
}
